package cn.core.test.modules.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.core.test.dao.mapper.Co_testMapper;
import cn.core.test.dao.po.Co_testPO;
import cn.osworks.aos.core.asset.AOSCons;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.id.AOSId;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;

/**
 * <b>自动生成测试[co_test] controller</b>
 * <p>
 * </p>
 * 
 * @author penghe
 * @date 2016-08-11 19:15:48
 */
@Service
public class Co_testService {
	@Autowired
	private Co_testMapper co_testmapper;

	/**
	 * 保存信息自动生成测试
	 * 
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto saveCo_test(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Co_testPO co_testPO = new Co_testPO();
		AOSUtils.copyProperties(inDto, co_testPO);
		if (!checkIsExist(co_testPO)) {
			outDto.setAppCode(AOSCons.ERROR);
			String msg = co_testPO.getName_() + "已被存在, 请重新输入。";
			outDto.setAppMsg(msg);
			return outDto;
		}
		co_testPO.setId_(AOSId.uuid());
		co_testmapper.insert(co_testPO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，用户新增成功。");
		return outDto;
	}

	/**
	 * 更新自动生成测试
	 * 
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto updateCo_test(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Co_testPO co_testPO = new Co_testPO();
		AOSUtils.copyProperties(inDto, co_testPO);
		co_testmapper.updateByKey(co_testPO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，用户新增成功。");
		return outDto;
	}

	/**
	 * 删除自动生成测试
	 * 
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto delete(Dto qDto) {
		Dto outDto = Dtos.newDto();
		String[] selections = qDto.getRows();
		int del = 0;
		for (String id_ : selections) {
			co_testmapper.deleteByKey(id_);
			del++;
		}
		String msg = "操作完成, ";
		if (del > 0) {
			msg = AOSUtils.merge(msg + "成功删除[{0}]个。", del);
		}
		outDto.setAppMsg(msg);
		return outDto;
	}

	/**
	 * 判断存在自动生成测试
	 */
	public boolean checkIsExist(Co_testPO co_testPO) {
		Dto calcDto = Dtos.newCalcDto("COUNT(id_)");
		calcDto.put("name_", co_testPO.getName_());
		Integer countInteger = Integer.valueOf(co_testmapper.calc(calcDto));
		boolean out = true;
		if (countInteger > 0) {
			out = false;
		}
		return out;
	}

}
